#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include "Log.hpp"
extern Log log;
std::string dictname = "./dict.txt";
std::string seg = ":";
class Init
{
public:
    bool Split(std::string& line, std::string* part1, std::string* part2)
    {
        size_t pos = line.find(seg);
        if(pos == std::string::npos)
        {
            return false;
        }
        *part1 = line.substr(0, pos);
        *part2 = line.substr(pos + 1);
        return true;
    }
    Init()
    {
        std::ifstream in(dictname);
        if(!in.is_open())
        {
            log(Fatal, "ifstream open %s error\n", dictname.c_str());
            exit(1);
        }
        std::string line;
        while(std::getline(in, line))
        {
            std::string part1;
            std::string part2;
            if(Split(line, &part1, &part2))
            {
                dict.insert(make_pair(part1, part2));
            }
        }

    }
    std::string Translation(const std::string& key)
    {
        std::unordered_map<std::string, std::string>::iterator it = dict.find(key);
        if(it == dict.end())  return "Unknown";
        else  return it->second;
    }


private:
    std::unordered_map<std::string, std::string> dict;
};